package com.jq.aution.common;

import com.jq.aution.controller.LoginController;
import com.jq.aution.utils.ServletUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

/**
 * 登录控制
 * Created by jiangqi on 2020/10/14
 */
@Aspect
@Component
public class LoginAspect {
    @Pointcut("execution(public String com.jq.aution.controller.*.*(..))" +
            "&& !execution(public * com.jq.aution.controller.IndexController.enterLogin(..))" +
            "&& !execution(public * com.jq.aution.controller.IndexController.enterProducts(..))" +
            "&& !execution(public * com.jq.aution.controller.IndexController.enterIndex(..))" +
            "&& !execution(public * com.jq.aution.controller.LoginController.login(..))" +
            "&& !execution(public * com.jq.aution.controller.LoginController.register(..))")
    public void LoginControl() {
    }
    @Around("LoginControl()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        HttpSession session = ServletUtil.getSession();
        Object result=null;
        System.out.println("执行登录控制");
        if(session.getAttribute("userId")!=null){
            try {
                result = joinPoint.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return result;
        }else{
            return "redirect:/index/login";
        }
    }
}
